//
//  CarNormalSecCell.swift
//  koolcar
//
//  Created by xiantian on 2023/11/3.
//

import UIKit
class CarNormalSecCell: UITableViewCell {
    var editFlag: Bool = false
    var bkgView: UIView = {
        let tempView = UIView()
        tempView.backgroundColor = .white
        return tempView
    }()
    var tableView: BaseTableView!
    var dataArray: [ShoppingCarListModel] = []
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        contentView.addSubview(bkgView)
        tableView = BaseTableView.init(frame: CGRect.zero, style: .plain)
        tableView.register(ShoppingCarCell.self, forCellReuseIdentifier: "ShoppingCarCell")
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = UIColor.white
        tableView.separatorStyle = .none
        tableView.isScrollEnabled = false
        tableView.contentInset = UIEdgeInsets.zero
        tableView.estimatedRowHeight = 120
        tableView.rowHeight = UITableViewAutomaticDimension
        bkgView.addSubview(tableView)
        self.addSnpLayout()
    }
    
    private func addSnpLayout(){
        bkgView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.width.equalTo(kScreenWidth)
            make.top.equalTo(0)
            make.bottom.equalTo(0).priority(.high)
        }
        self.tableView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.top.equalTo(10)
            make.height.equalTo(80)
            make.bottom.equalTo(bkgView.snp.bottom)
        }
    }
    
    ///赋值
    func setCarNormalSecCell(data: [ShoppingCarListModel]) {
        self.dataArray = data
        self.tableView.frame.size.height = 9999
        self.tableView.reloadData()
        self.tableView.layoutSubviews()
        self.tableView.layoutIfNeeded()
        let tempH = self.tableView.contentSize.height
        self.tableView.snp.updateConstraints { make in
            make.height.equalTo(tempH)
        }
        self.updateConstraints()
        self.layoutIfNeeded()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension CarNormalSecCell: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView()
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView()
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ShoppingCarCell", for: indexPath) as! ShoppingCarCell
        if self.dataArray.count > indexPath.row {
            let itemModel = self.dataArray[indexPath.row]
            cell.setShoppingCarCell(carModel: itemModel)
        }
        if self.editFlag {
            cell.numberView.isHidden = true
        }else{
            cell.numberView.isHidden = false
        }
        cell.selectionStyle = .none
        cell.clipsToBounds = true
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if self.editFlag {
            return
        }
        guard self.dataArray.count > indexPath.row else {
            return
        }
        let itemModel = self.dataArray[indexPath.row]
        let vc = ProductDetailsViewController()
        vc.id = itemModel.productId ?? 0
        self.viewController().navigationController?.pushViewController(vc, animated: true)
    }
}
